#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/11 10:26
# @Name    : test.py
# @email   : will05150707@163.com
# @Author  : wei.yan

import os
from datetime import datetime

from docx import Document


def merge_files(file1, file2):
    # 打开两个文件
    doc1 = Document(file1)
    doc2 = Document(file2)

    # 合并两个文件的内容
    for element in doc2.element.body:
        doc1.element.body.append(element)

    merge_file_name = os.path.splitext(file1)[0] + "_" + os.path.splitext(file2)[0] + "_" + datetime.now().strftime(
        '%Y%m%d%H%M%S') + ".docx"
    merged_file = os.path.join(os.path.dirname(__file__), merge_file_name)
    doc1.save(merged_file)

    return merge_file_name


if __name__ == '__main__':
    file1 = "word1.docx"
    file2 = "word2.docx"
    merge_files(file1, file2)
